package com.hellogood.yaohuo.client.setting;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hellogood.yaohuo.client.common.CommonMessages;
import com.hellogood.yaohuo.shared.entity.Groupqx;
import com.hellogood.yaohuo.shared.services.GroupQxService;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class GroupQxController {
	private VLayout layout = null;
	private ListGrid listGrid = null;
	private Button saveBtn = null;

	public GroupQxController() {
		initData();
		initView();
	}

	private void initData() {
		GroupQxService.Util.getInstance().getGroupQx(new GroupQxCallback());
	}

	private void initView() {

		ListGridField[] fields = new ListGridField[Groupqx.columnNames.length];
		for (int i = 0; i < Groupqx.columnNames.length; i++) {
			ListGridField field = new ListGridField(Groupqx.mks[i], Groupqx.columnNames[i]);
			if (Groupqx.mks[i].equals(Groupqx.BIAN_MA)) {
				field.setHidden(true);
			} else if (Groupqx.mks[i].equals(Groupqx.YONG_HU_ZU)) {
				field.setWidth(100);
			} else {
				field.setType(ListGridFieldType.BOOLEAN);
				field.setWidth("9%");
			}
			fields[i] = field;
		}
		listGrid = new ListGrid();
		listGrid.setFields(fields);
		listGrid.setWidth("100%");
		listGrid.setHeight("100%");
		listGrid.setCanEdit(true);

		saveBtn = new Button("保存");
		saveBtn.setTop(20);
		saveBtn.addClickHandler(new SaveGroupQxClickHandler());
		HLayout hLayout = new HLayout();
		hLayout.setAlign(Alignment.CENTER);
		hLayout.setMembersMargin(20);
		hLayout.setSize("100%", "50");
		hLayout.addMember(saveBtn);
		hLayout.setMembersMargin(10);

		layout = new VLayout();
		layout.setMembers(listGrid, hLayout);
	}

	public Canvas getCanvas() {
		return layout;
	}

	private class GroupQxCallback implements AsyncCallback<String[][]> {

		@Override
		public void onFailure(Throwable caught) {
			SC.say(caught.getMessage());
		}

		@Override
		public void onSuccess(String[][] result) {
			if (result != null) {
				Record[] records = new Record[result.length];
				for (int i = 0; i < result.length; i++) {
					Record r = new Record();
					for (int j = 0; j < result[i].length; j++) {
						String value = result[i][j];
						if (value.equals("true")) {
							r.setAttribute(Groupqx.mks[j], true);
						} else if (value.equals("false")) {
							r.setAttribute(Groupqx.mks[j], false);
						} else {
							r.setAttribute(Groupqx.mks[j], value);
						}
					}
					records[i] = r;
				}
				listGrid.setData(records);
			}
		}
	}

	private class SaveGroupQxClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			List<Groupqx> qxs = new ArrayList<Groupqx>();
			ListGridRecord[] records = listGrid.getRecords();
			if (records != null) {
				for (Record r : records) {
					String[] attrs = r.getAttributes();
					for (int i = 0; i < attrs.length; i++) {
						String attr = attrs[i];
						if (!attr.equals(Groupqx.BIAN_MA) && !attr.equals(Groupqx.YONG_HU_ZU)) {
							Groupqx qx = new Groupqx();
							qx.setGroupCode(r.getAttribute(Groupqx.BIAN_MA));
							if (r.getAttribute(attr).equals("true")) {								
								qx.setMkCode(attr);
								qxs.add(qx);
							}
						}
					}
				}
				GroupQxService.Util.getInstance().addGroupqx(qxs, new SaveGroupQxCallback());
			}
		}

	}

	private class SaveGroupQxCallback implements AsyncCallback<String> {

		@Override
		public void onFailure(Throwable caught) {
			SC.say(caught.getMessage());
		}

		@Override
		public void onSuccess(String result) {
			if (result != null && result.equals("")) {
				SC.say(CommonMessages.SaveSuccess);
			} else {
				SC.say(CommonMessages.SaveFailed + " " + result);
			}

		}

	}
}